package com.mmc.delay.ui.controller;

import com.alibaba.fastjson.JSON;
import com.mmc.delay.bean.DeadJob;
import com.mmc.delay.bean.Job;
import com.mmc.delay.ui.bean.Pager;
import com.mmc.delay.ui.service.RedisDelayAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/redisDelay/admin")
public class JobController {


    @Autowired
    private RedisDelayAdminService redisDelayAdminService;



    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/{page}.html")
    public String pageHtml(@PathVariable String page){
        return page;
    }

    @RequestMapping("/jobPoolData")
    @ResponseBody
    public Object jobPoolData(){
        return null;
    }

    @RequestMapping("/delayList")
    @ResponseBody
    public Pager<List<Job>> delayList(String topic, Integer page, Integer rows){
        if(StringUtils.isEmpty(topic)){
           return new Pager<>();
        }
        return redisDelayAdminService.queryDelayData(topic, page, rows);
    }

    @RequestMapping("/deadList")
    @ResponseBody
    public Pager<List<DeadJob>> deadList(String topic, Integer page, Integer rows){
        if(StringUtils.isEmpty(topic)){
            return new Pager<>();
        }
        return redisDelayAdminService.queryDeadData(topic, page, rows);
    }

    @RequestMapping("/stopTopic")
    @ResponseBody
    public String stopTopic(String topic){
        if(StringUtils.isEmpty(topic)){
            throw new IllegalArgumentException("topic不能为空");
        }
        redisDelayAdminService.stopTopic(topic);
        return "success";
    }

    @RequestMapping("/startTopic")
    @ResponseBody
    public String startTopic(String topic){
        if(StringUtils.isEmpty(topic)){
            throw new IllegalArgumentException("topic不能为空");
        }
        redisDelayAdminService.startTopic(topic);
        return "success";
    }

    @RequestMapping("/queryAllTopicStatus")
    @ResponseBody
    public Map<String,Boolean> queryAllTopicStatus(){
        return redisDelayAdminService.queryAllTopicStatus();
    }

    @RequestMapping("/deadMessageRetry")
    @ResponseBody
    public boolean deadMessageRetry(@RequestParam("deadJobs") String deadJobs){
        boolean result=true;
        List<DeadJob> deadJobList = JSON.parseArray(deadJobs, DeadJob.class);
        for (DeadJob deadJob:deadJobList){
            if(deadJob.getJobId()==null ||StringUtils.isEmpty(deadJob.getTopic())){
                throw new IllegalArgumentException("jobid或topic不能为空");
            }
            boolean b = redisDelayAdminService.deadMessageReTry(deadJob);
            result &=b;
        }
      return result;
    }




}
